package Viewer;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.IOException;
import java.net.URISyntaxException;

import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.JMenuItem;
import javax.swing.JPanel;
import javax.swing.KeyStroke;

/**
 * Main menu of the GeneScapes framework
 * @author Stefan Lorenz<br>
 * <br>29.11.2012
 */
@SuppressWarnings("serial")
public class FrameworkMenu extends JMenuBar {

    private Framework framework;

    /**
     * Main menu of the {@link Framework}
     * @param framework the GeneScapes Framework
     */
    public FrameworkMenu(Framework framework) {
	this.framework = framework;
	this.add(createFileMenu());
	this.add(createLookAndFeel());
	this.add(createHelpMenu());
    }

  
    /**
     * Returns the help menu
     * @return the help menu
     */
    private JMenu createHelpMenu() {
	JMenu help = new JMenu("Help");

	JMenuItem faq = new JMenuItem("Flash Tutorial");
	JMenuItem getting = new JMenuItem("Documentation...");
	JMenuItem about = new JMenuItem("About GeneScapes");

	help.add(faq);
	faq.addActionListener(new ActionListener() {
	    public void actionPerformed(ActionEvent e) {
		FrameworkMenu.this.framework.addPanelToDesktop(FrameworkMenu.this.framework.createFAQ(), "Flash Tutorial");
	    }
	});
	getting.addActionListener(new ActionListener() {
	    public void actionPerformed(ActionEvent e) {
	    	if(java.awt.Desktop.isDesktopSupported()){
	    		java.awt.Desktop desktop = java.awt.Desktop.getDesktop();
	    		if(desktop.isSupported(java.awt.Desktop.Action.BROWSE)){
	    			try {
						java.net.URI uri = new java.net.URI("http://code.google.com/p/genescapes/w/list/");
						desktop.browse(uri);
	    			} catch (URISyntaxException | IOException e1) {
						e1.printStackTrace();
					}
	    		}
	    	}
	    }
	});
	about.addActionListener(new ActionListener() {
	    public void actionPerformed(ActionEvent e) {
		JPanel panel = FrameworkMenu.this.framework.createAbout();
		FrameworkMenu.this.framework.addPanelToDesktop(panel, "About GeneScapes");
		
	    }
	});
	help.add(getting);
	help.addSeparator();
	help.add(about);

	this.add(help);
	return help;
    }

    /**
     * Returns the look and feel menu
     * @return the look and feel menu
     */
    private JMenu createLookAndFeel() {
	JMenu lookAndFeel = new JMenu("Look And Feel");
	final JMenuItem system = new JMenuItem("System");
	lookAndFeel.add(system);
	final JMenuItem nimbus = new JMenuItem("Nimbus");
	lookAndFeel.add(nimbus);
	final JMenuItem metal = new JMenuItem("Metal");
	lookAndFeel.add(metal);
	this.add(lookAndFeel);

	ActionListener ac = new ActionListener() {
	    @Override
	    public void actionPerformed(ActionEvent e) {
		if (e.getSource() == system)
		    FrameworkMenu.this.framework.updateLookAndFeel("system");
		else if (e.getSource() == nimbus)
		    FrameworkMenu.this.framework.updateLookAndFeel("nimbus");
		else if (e.getSource() == metal)
		    FrameworkMenu.this.framework.updateLookAndFeel("metal");
	    }
	};
	system.addActionListener(ac);
	nimbus.addActionListener(ac);
	metal.addActionListener(ac);
	return lookAndFeel;
    }

    /**
     * Returns the file menu
     * @return the file menu
     */
    private JMenu createFileMenu() {
	JMenu file = new JMenu("File");
	JMenuItem openLocal = new JMenuItem("Open Local File...");
	openLocal.setAccelerator(KeyStroke.getKeyStroke(java.awt.event.KeyEvent.VK_O, java.awt.Event.CTRL_MASK));
	openLocal.addActionListener(new ActionListener() {
	    public void actionPerformed(ActionEvent arg0) {
		FrameworkMenu.this.framework.openLocalFile();
	    }
	});
	file.add(openLocal);
	
	
	JMenuItem last = new JMenuItem("Open Last Opened...");
	last.setAccelerator(KeyStroke.getKeyStroke(java.awt.event.KeyEvent.VK_L, java.awt.Event.CTRL_MASK));
	last.addActionListener(new ActionListener() {
	    public void actionPerformed(ActionEvent arg0) {
	    	FrameworkMenu.this.framework.openLastFile();
	    }
	});
	file.add(last);
	

	JMenuItem openGenbankID = new JMenuItem("Download from NCBI...");
	openGenbankID.addActionListener(new ActionListener() {
	    public void actionPerformed(ActionEvent arg0) {
		FrameworkMenu.this.framework.openNcbiGenBankID();
	    }
	});
	file.add(openGenbankID);

	file.addSeparator();

	JMenuItem exit = new JMenuItem("Exit");
	exit.addActionListener(new ActionListener() {
	    public void actionPerformed(ActionEvent arg0) {
		System.exit(1);
	    }
	});
	file.add(exit);
	return file;
    }
}
